/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.navid.trafalgar.model.opengl2;

import com.jme3.asset.AssetManager;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.collision.CollisionResults;
import com.jme3.material.Material;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.Control;
import static com.navid.trafalgar.manager.EventManager.MILLESTONE_REACHED;
import com.navid.trafalgar.model.AMillestoneModel;

/**
 *
 * @author alberto
 */
public class MillestoneModel extends AMillestoneModel {

    private Material materialOn;
    private Material materialOff;
    private RigidBodyControl millestone;
    
    private Spatial spatial;
    

    public MillestoneModel(AssetManager assetManager) {
        super();
        spatial = assetManager.loadModel("Models/m1/m1.j3o");
        spatial.scale(3);
        this.attachChild(spatial);

        CollisionShape sceneShape =
                CollisionShapeFactory.createMeshShape(this);
        millestone = new RigidBodyControl(sceneShape, 0);
        this.addControl(millestone);
        millestone.setPhysicsLocation(new Vector3f(-200, 0, 100));
        
    }
    
   

    public void setMaterialOff(Material mat) {
        this.materialOff = mat;
    }

    public void setMaterialOn(Material mat) {
        this.materialOn = mat;
    }
    
    @Override
    public void setLocalTranslation(Vector3f pos){
        super.setLocalTranslation(pos);
        millestone.setPhysicsLocation(pos);
    }

    @Override
    public void update(float tpf) {
        if(state){
            this.setMaterial(materialOn);
        }else{
            this.setMaterial(materialOff);
        }
        
        CollisionResults results = new CollisionResults();
        for(Node currentCollidable: collidables){
            currentCollidable.collideWith(this.getWorldBound(), results);
        
        if (!state && results.size() > 0){
            state = true;
            eventManager.fireEvent(MILLESTONE_REACHED);
        }
        }
        
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setSpatial(Spatial spatial) {
    }

    public void render(RenderManager rm, ViewPort vp) {
    }
    
    public boolean isEnabled(){
        return true;
    }
    
    public void setEnabled(boolean value){
        //todo
    }
}
